package com.xxx.video.mine.activitys;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.xxx.video.R;
import com.xxx.video.commen.activitys.BaseActivity;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.views.IosSingleChoosePopupWindow;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.dynamic.activitys.DynamicDetailsActivity;
import com.xxx.video.mine.adapters.RecordingManagementListAdapter;
import com.xxx.video.mine.beans.DynamicListBean;
import com.xxx.video.mine.presenters.IRecordingManagementView;
import com.xxx.video.mine.presenters.RecordingManagementPresenter;

import java.util.ArrayList;

import butterknife.BindView;

public class RecordingManagementActivity extends BaseActivity<RecordingManagementPresenter>
        implements IRecordingManagementView, BaseRecycleViewAdapter.OnItemClickListener<DynamicListBean.PageBean.ListBean>, RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener {
    @BindView(R.id.rcv_recording_management_list)
    RefreshRecyclerView rcvRecordingManagementList;
    /**
     * page
     */
    private int page = 1;
    /**
     * adapter
     */
    private RecordingManagementListAdapter mRecordingManagementListAdapter;
    /**
     * start add audio request code
     */
    private static final int START_ADD_VIDEO_REQUEST_CODE = 201;

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        showActionBar();
        StatusBarUtil.getInstance().setStatusBarTransparent(this, true);
        setIsShowActionbarLine(true);
        setActionBarTitle(R.string.title_recording_management_activity);
        setActionBarRightIcon(R.drawable.ic_feed_back_add_photo_center);
    }

    @Override
    protected RecordingManagementPresenter createPresenter() {
        return new RecordingManagementPresenter(this, this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_recording_management;
    }

    @Override
    public void initData(Bundle extras) {
        showRootLayoutStatus(1);
        initRecordingManagementList();
        getPresenter().getDynamicList(page);
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initRecordingManagementList() {
        rcvRecordingManagementList.setLayoutManager(new LinearLayoutManager(this));
        mRecordingManagementListAdapter = new RecordingManagementListAdapter(this);
        rcvRecordingManagementList.setAdapter(mRecordingManagementListAdapter);
        rcvRecordingManagementList.setOnRecyclerViewRefreshOrLoadMoreListener(this);
        mRecordingManagementListAdapter.setOnItemClickListener(this);
    }

    @Override
    public void initData2View(DynamicListBean successOb) {
        rcvRecordingManagementList.resetState();
        showRootLayoutStatus(1);
        if (page == 1) {
            mRecordingManagementListAdapter.setData(successOb.page.list);
            if(ListUtils.isEmpty(successOb.page.list)){
                showRootLayoutStatus(2);
            }
        } else {
            mRecordingManagementListAdapter.addData(successOb.page.list);
        }
    }

    @Override
    public void showDeleteDialog(String id, int position) {
        IosSingleChoosePopupWindow iosSingleChooseDialog = new IosSingleChoosePopupWindow(this);
        ArrayList<String> stringArrayList = new ArrayList<>();
        stringArrayList.add(StringUtils.getInstance().getString(R.string.dialog_choose_confirm));
        iosSingleChooseDialog.setData(StringUtils.getInstance().getString(R.string.dialog_choose_is_delete_dynamic), StringUtils.getInstance().getString(R.string.dialog_choose_cancel), stringArrayList);
        iosSingleChooseDialog.setOnIosSingleChooseItemClickListener((string, position1, type) -> {
            if (type == 0) {
                /*
                 * confirm
                 */
                getPresenter().deleteDynamic(id, position);
            }
        });
        iosSingleChooseDialog.showAtLocation(getWindow().getDecorView());
        lightof();
        iosSingleChooseDialog.setOnDismissListener(this::lighton);
    }

    @Override
    public void deleteDynamicSuccess(int position) {
        if (mRecordingManagementListAdapter != null) {
            mRecordingManagementListAdapter.removeDataByPosition(position);
        }
    }

    @Override
    public void onActionBarRightIconClick() {
        super.onActionBarRightIconClick();
        startAddAudioActivity();
    }

    @Override
    public void startAddAudioActivity() {
        intent2Activity(AddAudioActivity.class, START_ADD_VIDEO_REQUEST_CODE);
    }

    @Override
    public void startVideoDetailsActivity(String videoId) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", videoId);
        intent2Activity(DynamicDetailsActivity.class, bundle);
    }

    @Override
    public void onItemClick(DynamicListBean.PageBean.ListBean listBean, int position, int type) {
        if (type == 1) {
            showDeleteDialog(listBean.fId, position);
        } else {
            startVideoDetailsActivity(listBean.fId);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == START_ADD_VIDEO_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                page = 1;
                getPresenter().getDynamicList(page);
            }
        }
    }

    @Override
    public void onRecyclerviewRefresh() {
        page = 1;
        getPresenter().getDynamicList(page);
    }

    @Override
    public void onRecyclerviewLoadMore() {
        page++;
        getPresenter().getDynamicList(page);
    }
}
